<template>
	<div class="count">
		<h1>当前求和为：{{sum}}</h1>
		<h3>当前求和放大10倍后：{{bigSum}}</h3>
		<h3>学校名称：{{school}}</h3>
		<h3>学校地址：{{address}}</h3>
		<h3>完整地址：{{fullAddress}}</h3>
		<h3>所学学科：{{subject}}</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment">+</button>
		<button @click="decrement">-</button>
		<button @click="incrementOdd">当前求和为奇数再+</button>
		<button @click="incrementWait">等一等再+</button>
		<button @click="incrementServer">问问服务器再+</button>
		<!-- <button @click="test">测试一下mapState有啥用</button> -->
	</div>
</template>

<script>
	import {mapState,mapGetters} from 'vuex'
	export default {
		name:'Count',
		data() {
			return {
				n:1 //用户选择的数字
			}
		},
		computed:{
			...mapState(['sum','school','address','subject']),

			//mapGetters的第一种写法
			//...mapGetters({dahe:'bigSum',wandizhi:'fullAddress'})

			//mapGetters的第二种写法
			...mapGetters(['bigSum','fullAddress'])
		},
		methods:{
			
			// 测试mapState
			test(){
				let result = mapState({
					he:'sum',
					xuexiao:'school',
					dizhi:'address',
					xueke:'subject'
				})
				console.log(result)
			},
			//加
			increment(){
				//联系对应的mutation函数，触发JIA（即：直接联系厨师）
				this.$store.commit('JIA',this.n)
			},
			//减
			decrement(){
				//联系对应的mutation函数，触发JIAN（即：直接联系厨师）
				this.$store.commit('JIAN',this.n)
			},
			//奇数再加
			incrementOdd(){
				this.$store.dispatch('jiaOdd',this.n)
			},
			//等等再加
			incrementWait(){
				this.$store.dispatch('jiaWait',this.n)
			},
			//联系服务器再加
			incrementServer(){
				this.$store.dispatch('jiaServer')
			}
		},
	}
</script>

<style scoped>
	.count{
		background-color: skyblue;
		padding: 20px;
	}
	button{
		margin-left: 10px;
	}
</style>